package com.gjy.rocketmq.consumer;

import com.gjy.rocketmq.RocketUtil;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-01-29 10:34:44
 */
public class SimpleConsumer {

    private static final Logger logger = LoggerFactory.getLogger(SimpleConsumer.class);

    public static void main(String[] args) throws Exception {

        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(RocketUtil.ConsumerGroup);
        consumer.setNamesrvAddr(RocketUtil.NAME_SRV);
//        consumer.setMessageModel(MessageModel.BROADCASTING);
        consumer.subscribe(RocketUtil.TOPIC_TEST, "*");

        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                logger.info("{} - Receive Message: {}", Thread.currentThread().getName(),
                        msgs);
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });

        consumer.start();
        logger.info("Consumer Started");
    }
}
